package org.sanmao.wyj.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.sanmao.wyj.domain.Order;
import org.springframework.security.core.parameters.P;

import java.util.List;

@Mapper
public interface OrderDao extends BaseMapper<Order> {

    @Select("select * from orders")
    List<Order> findAllOrders();

    @Select("select * from orders where uid = ${id}")
    List<Order> findOrderByUid(int id);

    @Select("select * from orders where uid = ${id} and type = ${type}")
    IPage<Order>  findOrderByUidAndType(IPage<Order> page,@Param("id") int id, @Param("type")int type);

    @Select("select * from orders where content like CONCAT('%', #{con}, '%')")
    IPage<Order> fuzzyFindOrders(IPage<Order> page,@Param("con")String con);

    @Select("select * from orders where uid = ${userId} and type = ${type} and content like CONCAT('%', #{con}, '%')")
    IPage<Order> fuzzyFindOrderByConAndUidAndType(IPage<Order> page,@Param("userId") int userId,@Param("con") String con, @Param("type") int type);

    @Select("select o.*,c.content as conment from orders as o left join conment as c on o.cid = c.cid where o.state = ${state} and o.uid=${id}")
    IPage<Order> findOrderByState(IPage<Order> page, @Param("state") int state,@Param("id")int id);

    @Select("select * from orders where type = ${type} and state = 1")
    IPage<Order> findOrderByType(IPage<Order> page,@Param("type") int type);

    @Select("select * from orders where  type = ${type} and state = 1 and content like CONCAT('%', #{con}, '%')")
    IPage<Order> fuzzyFindOrderByConAndType(IPage<Order> page, @Param("con") String con, @Param("type") int type);

    @Select("SELECT o.*,w.name as worker_name,c.content as conment,w.phonenum as worker_phonenum,u.name as user_name,u.phonenum as user_phonenum from orders AS o left join worker AS w  on o.wid = w.wid left  join user as u on u.uid = o.uid left join conment AS c on c.cid = o.cid where o.oid = ${oid}")
    Order findFullOrderByOid(int oid);

    @Select("select o.*,c.content as conment from orders as o left join conment as c on o.cid = c.cid where o.state = ${state} and o.wid = #{wid}")
    IPage<Order> workerFindOrderByState(IPage<Order> page, @Param("state") int state,@Param("wid")int wid);

    @Select("select u.phonenum as user_phonenum from orders join user as u on orders.uid = u.uid where orders.oid = ${oid}")
    String findUserPhonenumByOid(int oid);

    @Select("select w.phonenum as worker_phonenum from orders join worker as w on orders.wid = w.wid where orders.oid = ${oid}")
    String findWorkerPhonenumByOid(int oid);

    @Select("select * from orders where uid = ${userId} and state = ${state} and content like CONCAT('%', #{con}, '%')")
    IPage<Order> fuzzyFindOrderByConAndState(IPage<Order> page, @Param("con") String con, @Param("state") int state,@Param("userId")int id);
}
